<template>
  <van-nav-bar title="欢迎登陆" />
  <div class="img">
    <img src="@/images/login_03.jpg" alt="">
  </div>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]" />
      <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]" />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        登录
      </van-button>
      <van-button style="color: #157dfa;" type="default" round block url="register">注册</van-button>
    </div>
  </van-form>
</template>

<script setup>
import { showNotify } from 'vant';
import { ref } from 'vue';
import { loginApi } from '@/api/api'
import { useRouter } from 'vue-router';
const router = useRouter()
let username = ref('')
let password = ref('')
const onSubmit = async () => {
  const res = await loginApi(username.value, password.value)
  console.log('登录请求', res)
  if (res.data.code === 0) {
    localStorage.setItem('token', res.data.data.token)
    localStorage.setItem('username', res.data.data.username)
    showNotify({
      message: '登录成功',
      type: 'success',
      duration: 1500,
      onClose() {
        router.push({ path: '/home/my' })
      }
    });
  } else {
    showNotify({
      message: '登录失败',
      type: 'danger',
      duration: 1500,
    })
  }
}
</script>

<style lang="scss">
.img {
  margin-top: 100px;
  margin-left: 100px;

  img {
    width: 150px;
    height: 120px;
    border-radius: 50%;
  }
}

.van-button {
  margin-top: 10px;
}
</style>